package com.xiaochong.tir.api.service;

import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.api.component.RedisCacheComponent;
import com.xiaochong.tir.common.data.mapper.ConceptCategoryMapper;
import com.xiaochong.tir.common.data.mapper.ExchangeInfoMapper;
import com.xiaochong.tir.common.data.mapper.ExchangeTransactionpairMiddleMapper;
import com.xiaochong.tir.common.data.po.*;
import com.xiaochong.tir.common.data.vo.ApiExchangeInfoSeoVo;
import com.xiaochong.tir.common.data.vo.ApiResultVo;
import com.xiaochong.tir.common.data.vo.ApiSiteMapVo;
import com.xiaochong.tir.common.util.ApiResultCodeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.CountDownLatch;

/**
 * @Description: 站点地图
 * @Author: org
 * @CreateDate: 2018/8/13 9:24
 */
@Service
public class SeoSiteMapService {

    @Autowired
    private RedisCacheComponent redisCacheComponent;
    @Autowired
    private ExchangeInfoMapper exchangeInfoMapper;
    @Autowired
    private ExchangeTransactionpairMiddleMapper exchangeTransactionpairMiddleMapper;
    @Autowired
    private ConceptCategoryMapper conceptCategoryMapper;
    /**
     * 站点地图-货币
     * @return
     */
    public ApiResultVo getCoinUrlList() {
        ApiResultVo resultVo = new ApiResultVo();
        resultVo.setDataStamp(System.currentTimeMillis());
        resultVo.setTradeId(LogTrace.getTrace().getTraceId());
        try {
            List<CurrencyInfo> currencyInfoList = redisCacheComponent.getCurrencyList();
            List<ApiSiteMapVo> vo = new ArrayList<>();
            ApiSiteMapVo apiSiteMapVo;
            if(currencyInfoList != null && currencyInfoList.size() > 0){
                for(CurrencyInfo currencyInfo : currencyInfoList){
                    apiSiteMapVo = new ApiSiteMapVo();
                    String text = currencyInfo.getSymbol()+"-"+currencyInfo.getCoinNameCn();
                    String url = "diagnosis/"+currencyInfo.getCmcId()+"/"+text;
                    apiSiteMapVo.setUrl(url);
                    apiSiteMapVo.setText(text);
                    vo.add(apiSiteMapVo);
                }
            }
            if(vo.size() > 0){
                resultVo.setData(vo);
                resultVo.setCode(ApiResultCodeUtils.BASE_SUCCESS.getCode());
                resultVo.setMessage(ApiResultCodeUtils.BASE_SUCCESS.getMsg());
            }else{
                resultVo.setCode(ApiResultCodeUtils.SYSTEM_NULL.getCode());
                resultVo.setMessage(ApiResultCodeUtils.SYSTEM_NULL.getMsg());
            }
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("站点地图-货币",e);
            resultVo.setCode(ApiResultCodeUtils.SYSTEM_ERROR.getCode());
            resultVo.setMessage(ApiResultCodeUtils.SYSTEM_ERROR.getMsg());
        }
        return resultVo;
    }


    public ApiResultVo getPairUrlList() {
        ApiResultVo resultVo = new ApiResultVo();
        resultVo.setDataStamp(System.currentTimeMillis());
        resultVo.setTradeId(LogTrace.getTrace().getTraceId());
        try {
            Map<String,List<ApiSiteMapVo>> map = new HashMap<>();
            ApiSiteMapVo apiSiteMapVo;
            //查询所有交易所
            ExchangeInfo exchangeInfo=new ExchangeInfo();
            exchangeInfo.setEnableFlag( true );
            exchangeInfo.setDeleteFlag( false );
            exchangeInfo.setKlineStatus( true );
            List<ExchangeInfo> infoList=exchangeInfoMapper.selectByEntityList( exchangeInfo );
            for(ExchangeInfo exchange : infoList){
                List<ApiSiteMapVo> vo = new ArrayList<>();
                ExchangeTransactionpairMiddle exchangeTransactionpairMiddle=new ExchangeTransactionpairMiddle();
                exchangeTransactionpairMiddle.setEnableFlag( true );
                exchangeTransactionpairMiddle.setExchangeId( exchange.getId() );
                List<ExchangeTransactionpairMiddle> exchangeMiddleList = exchangeTransactionpairMiddleMapper.selectByEntityList( exchangeTransactionpairMiddle );
                for(ExchangeTransactionpairMiddle pair :exchangeMiddleList){
                    apiSiteMapVo = new ApiSiteMapVo();
                   // String text = currencyInfo.getSymbol()+"-"+currencyInfo.getCoinNameCn();
                    String url = "kline/"+exchange.getExchangeName()+"/"+pair.getTransactionName().replace("/","_");
                    apiSiteMapVo.setUrl(url);
                    apiSiteMapVo.setText(pair.getTransactionName());
                    vo.add(apiSiteMapVo);
                }
                map.put(exchange.getExchangeName(),vo);
            }
            if(map.size() > 0){
                resultVo.setData(map);
                resultVo.setCode(ApiResultCodeUtils.BASE_SUCCESS.getCode());
                resultVo.setMessage(ApiResultCodeUtils.BASE_SUCCESS.getMsg());
            }else{
                resultVo.setCode(ApiResultCodeUtils.SYSTEM_NULL.getCode());
                resultVo.setMessage(ApiResultCodeUtils.SYSTEM_NULL.getMsg());
            }
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("站点地图-交易对",e);
            resultVo.setCode(ApiResultCodeUtils.SYSTEM_ERROR.getCode());
            resultVo.setMessage(ApiResultCodeUtils.SYSTEM_ERROR.getMsg());
        }
        return resultVo;
    }

    public ApiResultVo getConceptUrlList() {
        ApiResultVo resultVo = new ApiResultVo();
        resultVo.setDataStamp(System.currentTimeMillis());
        resultVo.setTradeId(LogTrace.getTrace().getTraceId());
        try {
            ConceptCategory conceptCategory = new ConceptCategory();
            conceptCategory.setEnableFlag(true);
            conceptCategory.setDeleteFlag(false);
            List<ConceptCategory> conceptCategoryList = conceptCategoryMapper.selectByEntityList(conceptCategory);
            List<ApiSiteMapVo> vo = new ArrayList<>();
            ApiSiteMapVo apiSiteMapVo;
            if(conceptCategoryList != null && conceptCategoryList.size() > 0){
                for(ConceptCategory category : conceptCategoryList){
                    apiSiteMapVo = new ApiSiteMapVo();
                    String url = "/concept/detail/"+category.getId()+"/"+category.getConceptCategoryName();
                    apiSiteMapVo.setUrl(url);
                    apiSiteMapVo.setText(category.getConceptCategoryName());
                    vo.add(apiSiteMapVo);
                }
            }
            if(vo.size() > 0){
                resultVo.setData(vo);
                resultVo.setCode(ApiResultCodeUtils.BASE_SUCCESS.getCode());
                resultVo.setMessage(ApiResultCodeUtils.BASE_SUCCESS.getMsg());
            }else{
                resultVo.setCode(ApiResultCodeUtils.SYSTEM_NULL.getCode());
                resultVo.setMessage(ApiResultCodeUtils.SYSTEM_NULL.getMsg());
            }
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("站点地图-概念",e);
            resultVo.setCode(ApiResultCodeUtils.SYSTEM_ERROR.getCode());
            resultVo.setMessage(ApiResultCodeUtils.SYSTEM_ERROR.getMsg());
        }
        return resultVo;
    }

    public ApiResultVo getExchangeInfoByPairName(String exchangeName, String pairName) {
        ApiResultVo resultVo = new ApiResultVo();
        resultVo.setDataStamp(System.currentTimeMillis());
        resultVo.setTradeId(LogTrace.getTrace().getTraceId());
        try {
            if(StringUtils.isBlank(exchangeName) || StringUtils.isBlank(pairName)){
                resultVo.setCode(ApiResultCodeUtils.PARAMS_ERROR.getCode());
                resultVo.setMessage(ApiResultCodeUtils.PARAMS_ERROR.getMsg());
                return resultVo;
            }
            //根据交易所名称和交易对名称查询交易所id、交易对id、和中间表id
            Map<String,Object> params = new HashMap<>();
            params.put("exchangeName",exchangeName);
            params.put("pairName",pairName);
            ApiExchangeInfoSeoVo vo = exchangeTransactionpairMiddleMapper.getExchangeInfoByPairName(params);
            if(vo != null){
                resultVo.setData(vo);
                resultVo.setCode(ApiResultCodeUtils.BASE_SUCCESS.getCode());
                resultVo.setMessage(ApiResultCodeUtils.BASE_SUCCESS.getMsg());
            }else{
                resultVo.setCode(ApiResultCodeUtils.SYSTEM_NULL.getCode());
                resultVo.setMessage(ApiResultCodeUtils.SYSTEM_NULL.getMsg());
            }
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("站点地图-搜索交易所id",e);
            resultVo.setCode(ApiResultCodeUtils.SYSTEM_ERROR.getCode());
            resultVo.setMessage(ApiResultCodeUtils.SYSTEM_ERROR.getMsg());
        }
        return resultVo;
    }
}
